<template>
  <div class="family-member-group">
    <div class="tree-family-unit">
      <template v-if="node.spouse">
        <!-- 夫妻节点 -->
        <div class="tree-person-container">
          <div class="tree-person" :class="{'male': node.member.sex === '男', 'female': node.member.sex === '女'}">
            <div class="person-avatar">
              <img v-if="node.member.avatar" :src="node.member.avatar" alt="头像">
              <i v-else class="fas" :class="node.member.sex === '男' ? 'fa-male' : 'fa-female'"></i>
            </div>
            <div class="person-info">
              <div class="person-name">{{ node.member.name }}</div>
              <div class="person-dates">
                {{ formatDate(node.member.birthDate) }} - {{ node.member.deathDate ? formatDate(node.member.deathDate) : '至今' }}
              </div>
            </div>
          </div>
          <div class="tree-connector"></div>
          <div class="tree-person" :class="{'male': node.spouse.sex === '男', 'female': node.spouse.sex === '女'}">
            <div class="person-avatar">
              <img v-if="node.spouse.avatar" :src="node.spouse.avatar" alt="头像">
              <i v-else class="fas" :class="node.spouse.sex === '男' ? 'fa-male' : 'fa-female'"></i>
            </div>
            <div class="person-info">
              <div class="person-name">{{ node.spouse.name }}</div>
              <div class="person-dates">
                {{ formatDate(node.spouse.birthDate) }} - {{ node.spouse.deathDate ? formatDate(node.spouse.deathDate) : '至今' }}
              </div>
            </div>
          </div>
        </div>
      </template>
      <template v-else>
        <!-- 单人节点 -->
        <div class="tree-person" :class="{'male': node.member.sex === '男', 'female': node.member.sex === '女'}">
          <div class="person-avatar">
            <img v-if="node.member.avatar" :src="node.member.avatar" alt="头像">
            <i v-else class="fas" :class="node.member.sex === '男' ? 'fa-male' : 'fa-female'"></i>
          </div>
          <div class="person-info">
            <div class="person-name">{{ node.member.name }}</div>
            <div class="person-dates">
              {{ formatDate(node.member.birthDate) }} - {{ node.member.deathDate ? formatDate(node.member.deathDate) : '至今' }}
            </div>
          </div>
        </div>
      </template>
    </div>
    
    <!-- 子女连接线 -->
    <div v-if="node.children && node.children.length > 0" class="children-connector">
      <div class="vertical-line"></div>
    </div>
    
    <!-- 递归显示子女 -->
    <div v-if="node.children && node.children.length > 0" class="children-container">
      <div v-for="(childNode, childIndex) in node.children" :key="childIndex" class="child-branch">
        <div class="horizontal-line"></div>
        <family-tree-node :node="childNode" :format-date="formatDate" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'FamilyTreeNode',
  props: {
    node: {
      type: Object,
      required: true
    },
    formatDate: {
      type: Function,
      required: true
    }
  }
}
</script>

<style scoped>
.family-member-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
  transition: all 0.3s ease;
}

.tree-family-unit {
  display: flex;
  justify-content: center;
  margin-bottom: 10px;
}

.tree-person-container {
  display: flex;
  align-items: center;
}

.tree-connector {
  width: 50px;
  height: 2px;
  background-color: #333;
  margin: 0 10px;
}

.tree-person {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 12px;
  border-radius: 8px;
  min-width: 140px;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease;
  cursor: pointer;
}

.tree-person:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.tree-person.male {
  background-color: #e6f7ff;
  border: 2px solid #1890ff;
}

.tree-person.female {
  background-color: #fff0f6;
  border: 2px solid #eb2f96;
}

.person-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f0f0f0;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 8px;
  overflow: hidden;
  border: 2px solid #fff;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.person-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.person-avatar i {
  font-size: 28px;
}

.person-avatar i.fa-male {
  color: #1890ff;
}

.person-avatar i.fa-female {
  color: #eb2f96;
}

.person-info {
  text-align: center;
  width: 100%;
}

.person-name {
  font-weight: bold;
  margin-bottom: 5px;
  font-size: 1.1em;
}

.person-dates {
  font-size: 0.85em;
  color: #666;
}

.children-connector {
  height: 30px;
  width: 2px;
  background-color: #333;
  margin: 5px auto 15px;
}

.children-container {
  display: flex;
  justify-content: center;
  gap: 40px;
  flex-wrap: wrap;
  padding: 0 20px;
}

.child-branch {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.horizontal-line {
  width: 2px;
  height: 25px;
  background-color: #333;
  margin-bottom: 15px;
}

/* 添加媒体查询，优化移动设备上的显示 */
@media (max-width: 768px) {
  .children-container {
    gap: 20px;
    padding: 0 10px;
  }
  
  .tree-person {
    min-width: 120px;
    padding: 8px;
  }
  
  .person-avatar {
    width: 50px;
    height: 50px;
  }
  
  .person-name {
    font-size: 1em;
  }
}
</style> 